#include "../include/gudb.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

static gudb* global_db = NULL;

void signal_handler(int sig) {
    if (global_db) {
        printf("\n正在关闭服务器...\n");
        gudb_server_stop(global_db);
        gudb_close(global_db);
        global_db = NULL;
    }
    exit(0);
}

int main() {
    // 设置信号处理
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    
    printf("=== GuDB MySQL 服务器启动示例 ===\n\n");
    
    // 初始化数据库
    printf("📊 初始化数据库...\n");
    global_db = gudb_open("/tmp/server_demo_db", GU_OPEN_CREATE, "admin", "admin");
    if (!global_db) {
        printf("❌ 数据库初始化失败\n");
        return -1;
    }
    printf("✅ 数据库初始化成功\n\n");
    
    // 创建示例表和数据
    printf("📋 创建示例表...\n");
    gudb_txn* txn = gudb_begin(global_db, GU_TXN_RC);
    if (txn) {
        result_t* result = NULL;
        
        // 创建产品表
        gudb_exec(txn, 
            "CREATE TABLE products ("
            "id INT PRIMARY KEY, "
            "name VARCHAR(100), "
            "price DECIMAL(10,2), "
            "category VARCHAR(50)"
            ")", NULL, &result);
        
        // 插入示例数据
        gudb_exec(txn, "INSERT INTO products VALUES (1, '笔记本电脑', 5999.99, '电子产品')", NULL, &result);
        gudb_exec(txn, "INSERT INTO products VALUES (2, '办公椅', 899.50, '家具')", NULL, &result);
        gudb_exec(txn, "INSERT INTO products VALUES (3, '编程书籍', 79.90, '图书')", NULL, &result);
        
        gudb_commit(txn);
        if (result) gudb_free_result(result);
        printf("✅ 示例数据创建成功\n\n");
    }
    
    // 启动网络服务器
    printf("🌐 启动 MySQL 协议服务器...\n");
    printf("   端口: 3316\n");
    printf("   绑定地址: 0.0.0.0 (所有接口)\n\n");
    
    int result = gudb_server_start(global_db, "0.0.0.0", 3316);
    
    if (result == GU_OK) {
        printf("✅ 服务器启动成功！\n\n");
        
        printf("📱 连接信息:\n");
        printf("   主机: localhost (或你的服务器IP)\n");
        printf("   端口: 3316\n");
        printf("   用户: admin\n");
        printf("   密码: admin\n\n");
        
        printf("💻 连接命令:\n");
        printf("   mysql -h localhost -P 3316 -u admin -padmin\n\n");
        
        printf("🔍 测试查询:\n");
        printf("   SELECT * FROM products;\n");
        printf("   SELECT name, price FROM products WHERE price > 100;\n");
        printf("   SELECT category, COUNT(*) FROM products GROUP BY category;\n\n");
        
        printf("📝 Python 连接示例:\n");
        printf("   import mysql.connector\n");
        printf("   conn = mysql.connector.connect(\n");
        printf("       host='localhost', port=3316,\n");
        printf("       user='admin', password='admin'\n");
        printf("   )\n\n");
        
        printf("⚠️  按 Ctrl+C 停止服务器\n");
        printf("================================================\n\n");
        
        // 保持服务器运行
        while (1) {
            sleep(1);
        }
    } else {
        printf("❌ 服务器启动失败，错误代码: %d\n", result);
        printf("\n可能的原因:\n");
        printf("  - 端口 3316 被其他程序占用\n");
        printf("  - 权限不足\n");
        printf("  - 系统资源不足\n\n");
        printf("解决方法:\n");
        printf("  netstat -ln | grep 3316  # 检查端口占用\n");
        printf("  lsof -ti:3316 | xargs kill -9  # 杀死占用进程\n");
        
        gudb_close(global_db);
        return -1;
    }
    
    return 0;
}